package com.service.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.common.utils.constant.Constants;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.MDC;

import java.time.LocalDateTime;

/**
 * @author zhaoj
 * @create 2021/9/1
 */
public class MyMetaObjectHandler implements  MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String userId = MDC.get(Constants.USER_ID);
        if (userId != null){
            this.strictInsertFill(metaObject, "creator", String.class, userId);
        }
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}
